/*
* Conditions Of Use
*
* This software was developed by employees of the Sigmatrix(Beijing) Corporation.
* This software is provided by sigmatrix as a service and is expressly
* provided "AS IS."  Sigmatrix MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY.  Sigmatrix does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement.
*
*/
package com.sigmatrix.h5.web.controller;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.sigmatrix.h5.enums.ResponseEnum;
import com.sigmatrix.h5.exception.ServiceException;
import com.sigmatrix.h5.request.Request;
import com.sigmatrix.h5.response.Response;
import com.sigmatrix.h5.service.DealerService;
import com.sigmatrix.h5.utils.Cons;
import com.sigmatrix.h5.web.controller.common.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 *@ClassName: DealersController
 *@Description: 经销商接口控制器
 *@author youjun
 *@date 2017年6月27日 上午10:35:19
 */
@Api(tags="经销商相关接口")
@RestController
@RequestMapping("/app/dealers")
public class DealersController extends BaseController {

	/**logger record*/
	private static final Logger LOGGER = LoggerFactory.getLogger(OutStorageController.class);
	
	/**经销商业务层接口*/
	@Autowired
	private transient DealerService dealerService;
	
	/**
	 *@Title: syncDealersList
	 *@Description: 同步经销商列表
	 *@return Response
	 *@author youjun
	 *@date 2017年6月27日 下午1:55:07
	 *@param request
	 *@param dealersVo
	 *@param result
	 *@return
	 */
	@ApiOperation(value="经销商同步")
	@PostMapping(value="/synclist")
	public Response syncDealer(@Valid @RequestBody final Request requestVo,
			final BindingResult result) {
		LOGGER.info("同步经销商列表接口请求参数={}",JSONObject.toJSONString(requestVo));
		try {
			if (result.hasErrors()) {
				final String errInfo = result.getAllErrors().get(0).getDefaultMessage();
				LOGGER.error("同步经销商列表接口参数验证失败{}", errInfo);
				return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, errInfo);
			}
			return dealerService.syncList(requestVo);
		} catch (ServiceException e) {
			LOGGER.error("同步经销商列表接口失败", e);
			return new Response(ResponseEnum.EXCEPTION);
		}
	}
}
